/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.database.tables;

import aiaudio.database.Family;
import aiaudio.database.qualifiers.DoubleQualifier;
import aiaudio.database.qualifiers.IntQualifier;
import aiaudio.database.qualifiers.StringQualifier;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author nastya
 */
public class RatingsListTable extends TableStructure {

    private static final Family COORDINATES = new Family("d");
    private static final Family RATINGS = new Family("r");
    public static final StringQualifier ARTIST = new StringQualifier("artist");
    public static final StringQualifier USER = new StringQualifier("user");
    public static final IntQualifier LISTEN_COUNT = new IntQualifier("listenCount");
    public static final DoubleQualifier INITIAL_RATING = new DoubleQualifier("initialRating");
    public static final DoubleQualifier NETWOTK_RATING = new DoubleQualifier("networkCalculatedRating");

    @Override
    public Family[] getAllFamilies() {
        return new Family[]{COORDINATES, RATINGS};
    }

    public static void addArtist(Put put, String id) {
        TableStructure.addDataSavePreviousRow(put, COORDINATES, ARTIST, id);
    }

    public static void addArtist(Put put, byte[] id) {
        addArtist(put, Bytes.toString(id));
    }

    public static String getArtist(Result result) {
        return TableStructure.getValue(result, COORDINATES, ARTIST);
    }

    public static byte[] getArtistAsBytes(Result result) {
        return TableStructure.getValueAsBytes(result, COORDINATES, ARTIST);
    }

    public static void addUser(Put put, String id) {
        TableStructure.addDataSavePreviousRow(put, COORDINATES, USER, id);
    }

    public static void addUser(Put put, byte[] id) {
        addUser(put, Bytes.toString(id));
    }

    public static String getUser(Result result) {
        return TableStructure.getValue(result, COORDINATES, USER);
    }

    public static byte[] getUserAsBytes(Result result) {
        return TableStructure.getValueAsBytes(result, COORDINATES, USER);
    }

    public static void addListenCount(Put put, int value) {
        TableStructure.addDataSavePreviousRow(put, RATINGS, LISTEN_COUNT, value);
    }

    public static int getListenCount(Result result) {
        return TableStructure.getValue(result, RATINGS, LISTEN_COUNT);
    }

    public static void addInitialRating(Put put, double value) {
        TableStructure.addDataSavePreviousRow(put, RATINGS, INITIAL_RATING, value);
    }
    public static void addInitialRating(Put put, byte[] value) {
        addInitialRating(put, Bytes.toDouble(value));
    }

    public static Double getInitialRating(Result result) {
        return TableStructure.getValue(result, RATINGS, INITIAL_RATING);
    }

    public static void addCalculatedRating(Put put, double value) {
        TableStructure.addDataSavePreviousRow(put, RATINGS, NETWOTK_RATING, value);
    }

    public static Double getCalculatedRating(Result result) {
        return TableStructure.getValue(result, RATINGS, NETWOTK_RATING);
    }

    public static Filter createByUserFilter(String userKey) {
        return createByUserFilter(Bytes.toBytes(userKey));
    }

    public static Filter createByUserFilter(byte[] userKey) {
        return new SingleColumnValueFilter(COORDINATES.getNameAsBytes(), USER.getNameAsBytes(), CompareFilter.CompareOp.EQUAL, userKey);
    }

    public static Filter createByArtistFilter(String artistKey) {
        return new SingleColumnValueFilter(COORDINATES.getNameAsBytes(), ARTIST.getNameAsBytes(), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(artistKey));
    }

    public static Filter createByArtistAnsUserFilter(String userKey, String artistKey) {
        Filter ratingByUserFilter = createByUserFilter(userKey);
        Filter ratingByArtistFilter = createByArtistFilter(artistKey);

        FilterList list = new FilterList();
        list.addFilter(ratingByUserFilter);
        list.addFilter(ratingByArtistFilter);

        return list;
    }
}
